#!/usr/bin/env python
#coding: utf-8

from msnp import Session,SessionCallbacks,ChatCallbacks,States
from msnp import error

class MsnListener(SessionCallbacks):
    def chat_started(self,chat):
        print "start chat"
        callbacks = MsnChatListener()
        chat.callbacks = callbacks
        callbacks.chat = chat
def process_command(passport_id,text):
    args = text.split()
    if len(args) == 0:
        return "say something"
    if args[0] == 'h' or args[0] == "help":
        return "this is help"
    return "you say %s"%("".join(args))

class MsnChatListener(ChatCallbacks):
    def message_received(self, passport_id, display_name, text, charset):   #收到消息的时候调用
        print '%s: %s' % (passport_id, text)
        res = process_command(passport_id, text)
        self.chat.send_message(res, charset)

    def state_changed(self,state):
        if state == States.ONLINE:
            print "you are now on line"

if __name__ == "__main__" : 
    """
    while True:
        try:
            msn = Session(MsnListener())
            msn.login("netbigzb@hotmail.com", "19830906")
            while True:
                try:
                    msn.process(chats=True)
                except error.Error,e:
                    print e.code,e.message
                    break
                except Exception,e:
                    print e
                    break
        except KeyboardInterrupt:
            print "Interrupted"
            break
        except error.Error,e:
            print e.code,e.message
            break
        except Exception,e:
            print e
            break
    """
    msn = Session(MsnListener())
    msn.login("netbigzb@hotmail.com", "19830906")
    msn.process(chats=True)
